package day17MySQLWork.dao;
import day17MySQLWork.domain.ZhangWuDomain;
import day17MySQLWork.tools.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;

public class ZhangWuDao {

    //获取数据库连接池，得到操作表数据的对象 QueryRunner
    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    /**
     * 查询所有
     */
    public List<ZhangWuDomain> selectAll() {
        String sql = "select * from gjp_zhangwu";
        try {
         
            return qr.query(sql, new BeanListHandler<ZhangWuDomain>(ZhangWuDomain.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 按条件查询账务方法
     * @param start
     * @param end
     * @return
     */
    public List<ZhangWuDomain> select(String start, String end) {
        String sql = "select * from gjp_zhangwu where createtime between ? and ?";
        Object[] params = {start, end};
        try {
            return qr.query(sql, new BeanListHandler<ZhangWuDomain>(ZhangWuDomain.class),
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 添加账务
     * @param zw
     */
    public void addZhangWu(ZhangWuDomain zw) {
        String sql = "insert into gjp_zhangwu(flname,money,zhanghu,createtime,description) values(?,?,?,?,?)";
        try {
            Object[] params =
                    {zw.getFlname(),zw.getMoney(),zw.getZhangHu(),zw.getCreatetime(),zw.getDescription()};
            qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
